

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.config.configFile &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../index.html" />
    <link rel="up" title="pyamp.config" href="../config.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../config.html" accesskey="U">pyamp.config</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.config.configFile</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The configFile module contains the ConfigFile class which provides</span>
<span class="sd">the ability to parse a configuration file for configured :class:`Option`</span>
<span class="sd">objects.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">exists</span>
<span class="kn">from</span> <span class="nn">ConfigParser</span> <span class="kn">import</span> <span class="n">ConfigParser</span>

<span class="kn">from</span> <span class="nn">pyamp.logging</span> <span class="kn">import</span> <span class="n">Loggable</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;ConfigFile&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="ConfigFile"><a class="viewcode-back" href="../../../_generated/pyamp_config_configFile.html#pyamp.config.configFile.ConfigFile">[docs]</a><span class="k">class</span> <span class="nc">ConfigFile</span><span class="p">(</span><span class="n">Loggable</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The ConfigFile class is responsible for parsing a configuration file</span>
<span class="sd">    with the given dictionary of sections and options, and variables.</span>

<span class="sd">    The ConfigFile class takes a dictionary of options which map the</span>
<span class="sd">    configuration section names to the list of configuration :class:`Option`</span>
<span class="sd">    objects associated with that section. It also takes a dictionary of</span>
<span class="sd">    variables mapping the name of the variable to the value for the variable.</span>
<span class="sd">    The variable names can then be used in a configuration file by adding</span>
<span class="sd">    a $ before the variable name. This value will then be replaced with the</span>
<span class="sd">    value assigned in the variables dictionary.</span>

<span class="sd">    This class implements the :class:`Loggable` interface. This class uses the</span>
<span class="sd">    :mod:`ConfigParser` module to parse the configuration file, and thus parses</span>
<span class="sd">    configuration files of the same format.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">configurationFile</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">variables</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">logger</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * configurationFile -- The configuration file to load</span>
<span class="sd">        * options -- The dictionary of supported options</span>
<span class="sd">        * variables -- The dictionary of supported variables</span>
<span class="sd">        * logger -- The logger</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Loggable</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">logger</span><span class="p">)</span>

        <span class="c"># Define the set of variables that can be used in the config file</span>
        <span class="c"># Note: Each variable must be used with a $ before it to indicate that</span>
        <span class="c">#       it is a config variable that should be replaced. Variable names</span>
        <span class="c">#       should also be all caps, and do not include spaces.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__variables</span> <span class="o">=</span> <span class="p">{}</span> <span class="k">if</span> <span class="n">variables</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">variables</span>

        <span class="c"># A dictionary mapping section names to the Option objects that each</span>
        <span class="c"># section contains</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__options</span> <span class="o">=</span> <span class="p">{}</span> <span class="k">if</span> <span class="n">options</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">options</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c"># Initialize all of the variables which will store options</span>
        <span class="c"># prior to parsing the configuration file</span>
        <span class="k">for</span> <span class="n">section</span><span class="p">,</span> <span class="n">sectionOptions</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__options</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="c"># Ensure that a list of options is given</span>
            <span class="n">objType</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__options</span><span class="p">[</span><span class="n">section</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">objType</span> <span class="o">!=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">list</span><span class="p">()):</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;Section [</span><span class="si">%s</span><span class="s">] must have a list of options &quot;</span> \
                                    <span class="s">&quot;not a &#39;</span><span class="si">%s</span><span class="s">&#39;!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">objType</span><span class="p">))</span>

            <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">sectionOptions</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">getName</span><span class="p">(),</span> <span class="n">option</span><span class="o">.</span><span class="n">getDefaultValue</span><span class="p">())</span>

        <span class="c"># Print all of the variables for debugging</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__variables</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Variable: [</span><span class="si">%s</span><span class="s">] = </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)),</span> <span class="n">level</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">configurationFile</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">configurationFile</span><span class="p">)</span>

<div class="viewcode-block" id="ConfigFile.getSettings"><a class="viewcode-back" href="../../../_generated/pyamp_config_configFile.html#pyamp.config.configFile.ConfigFile.getSettings">[docs]</a>    <span class="k">def</span> <span class="nf">getSettings</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the settings dictionary.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span>
</div>
<div class="viewcode-block" id="ConfigFile.get"><a class="viewcode-back" href="../../../_generated/pyamp_config_configFile.html#pyamp.config.configFile.ConfigFile.get">[docs]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">section</span><span class="p">,</span> <span class="n">option</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the value of the given configuration option from the given</span>
<span class="sd">        section.</span>

<span class="sd">        * section -- The section name</span>
<span class="sd">        * option -- The option</span>
<span class="sd">        * defaultValue -- The default value if the option does not exist</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">sectionOptions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="p">{})</span>
        <span class="k">return</span> <span class="n">sectionOptions</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">defaultValue</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="ConfigFile.parse"><a class="viewcode-back" href="../../../_generated/pyamp_config_configFile.html#pyamp.config.configFile.ConfigFile.parse">[docs]</a>    <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Parse a configuration file.</span>

<span class="sd">        * filename -- The configuration file to parse</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="n">exists</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__configParser</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__configParser</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>

            <span class="c"># Traverse all of the configuration sections found</span>
            <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__configParser</span><span class="o">.</span><span class="n">sections</span><span class="p">():</span>
                <span class="n">configSection</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getSectionOptions</span><span class="p">(</span><span class="n">section</span><span class="p">)</span>

                <span class="c"># Determine if the section exists before proceeding</span>
                <span class="k">if</span> <span class="n">configSection</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="c"># Create the section entry in our settings dictionary</span>
                    <span class="k">if</span> <span class="n">section</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="p">[</span><span class="n">section</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>

                    <span class="c"># Traverse all of the configuration options</span>
                    <span class="k">for</span> <span class="n">optionName</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__configParser</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="n">section</span><span class="p">):</span>
                        <span class="c"># Continue only if we found the current option</span>
                        <span class="k">if</span> <span class="n">optionName</span> <span class="ow">in</span> <span class="n">configSection</span><span class="p">:</span>
                            <span class="n">optionObj</span> <span class="o">=</span> <span class="n">configSection</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">optionName</span><span class="p">)</span>
                            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__configParser</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">section</span><span class="p">,</span>
                                                            <span class="n">optionName</span><span class="p">)</span>
                            <span class="n">finalValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getValue</span><span class="p">(</span><span class="n">optionObj</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

                            <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">optionName</span><span class="p">]</span> <span class="o">=</span> <span class="n">finalValue</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;Unknown configuration option &quot;</span> \
                                          <span class="s">&quot;[</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">]&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">optionName</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;Unknown configuration section [</span><span class="si">%s</span><span class="s">]&quot;</span> <span class="o">%</span> <span class="n">section</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">__getValue</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">optionObj</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the complete value for the given option.</span>

<span class="sd">        * option -- The option</span>
<span class="sd">        * value -- The value</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Replace all variables, and convert the value to the expected type</span>
        <span class="n">optionValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__replaceVariables</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">optionObj</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">optionValue</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__replaceVariables</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">optionValue</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Replace all variables found within the option value.</span>

<span class="sd">        * optionValue -- The value of the option</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Replace all of the variables with their corresponding replacements</span>
        <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">replacement</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__variables</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="n">variable</span> <span class="o">=</span> <span class="s">&quot;$</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">variable</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
            <span class="n">optionValue</span> <span class="o">=</span> <span class="n">optionValue</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">variable</span><span class="p">,</span> <span class="n">replacement</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">optionValue</span>

    <span class="k">def</span> <span class="nf">__getSectionOptions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">section</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return a dictionary mapping Option names to the actual Option</span>
<span class="sd">        objects for the given section.</span>

<span class="sd">        * section -- The section</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">optionsDict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="p">[]):</span>
            <span class="k">if</span> <span class="n">option</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">optionsDict</span><span class="p">[</span><span class="n">option</span><span class="o">.</span><span class="n">getName</span><span class="p">()]</span> <span class="o">=</span> <span class="n">option</span>

        <span class="k">return</span> <span class="n">optionsDict</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../config.html" >pyamp.config</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>